package com.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.admin.filter.aop.annotation.LogAnnotation;
import com.admin.entity.SysRole;
import com.admin.service.RolePermissionService;
import com.admin.service.RoleService;
import com.admin.service.SysRoleDeptService;
import com.admin.web.PageResp;
import com.admin.web.request.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;


/**
 * 角色管理模块
 */
@RequestMapping("/sys")
@RestController
@Api(tags = "组织模块-角色管理")
public class RoleController {

    @Resource
    private RoleService roleService;
    @Resource
    private SysRoleDeptService sysRoleDeptService;
    @Resource
    private RolePermissionService rolePermissionService;

    @PostMapping("/role")
    @ApiOperation(value = "新增角色接口")
    @LogAnnotation(title = "角色管理", action = "新增角色")
    @SaCheckPermission("sys:role:add")
    public void addRole(@RequestBody @Valid RoleAddReq roleAddReq) {
        roleService.addRole(roleAddReq);
    }

    @DeleteMapping("/role/{id}")
    @ApiOperation(value = "删除角色接口")
    @LogAnnotation(title = "角色管理", action = "删除角色")
    @SaCheckPermission("sys:role:deleted")
    public void deleted(@PathVariable("id") String id) {
        roleService.deletedRole(id);
    }

    @PutMapping("/role")
    @ApiOperation(value = "更新角色信息接口")
    @LogAnnotation(title = "角色管理", action = "更新角色信息")
    @SaCheckPermission("sys:role:update")
    public void updateDept(@RequestBody RoleUpdateReq roleUpdateReq) {
        roleService.updateRole(roleUpdateReq);
    }

    @PostMapping("/role/bindDept")
    @ApiOperation(value = "绑定角色部门接口")
    @LogAnnotation(title = "角色管理", action = "绑定角色部门信息")
    @SaCheckPermission("sys:role:bindDept")
    public void bindDept(@RequestBody DeptBindRoleReq deptBindRoleReq) {
        sysRoleDeptService.bindDept(deptBindRoleReq);
    }

    @GetMapping("/role/{id}")
    @ApiOperation(value = "查询角色详情接口")
    @LogAnnotation(title = "角色管理", action = "查询角色详情")
    @SaCheckPermission("sys:role:detail")
    public SysRole detailInfo(@PathVariable("id") String id) {
        return roleService.detailInfo(id);
    }

    @PostMapping("/roles")
    @ApiOperation(value = "分页获取角色信息接口")
    @LogAnnotation(title = "角色管理", action = "分页获取角色信息")
    @SaCheckPermission("sys:role:list")
    public PageResp<SysRole> pageInfo(@RequestBody RolePageReq rolePageReq) {
        return roleService.page(rolePageReq);
    }

    @PostMapping("/role/permission")
    @ApiOperation(value = "修改或者新增角色菜单权限接口")
    @LogAnnotation(title = "角色和菜单关联接口", action = "修改或者新增角色菜单权限")
    @SaCheckPermission(value = {"sys:role:update", "sys:role:add"}, mode = SaMode.OR)
    public void operationRolePermission(@RequestBody @Valid RolePermissionReq vo) {
        rolePermissionService.addRolePermission(vo);
    }
}
